﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.IO;
using PDFLibNet;
using System.Diagnostics;
using System.Threading;

namespace WinFormStudy
{
    public partial class Form_PDF2Jpg : Form
    {
        public Form_PDF2Jpg()
        {            
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;//这一行是关键
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Show();
        }
        List<string> ipList = new List<string>();
        private void Form1_Load(object sender, EventArgs e)
        {
            pbStatus.Visible = false;
            ////获取本地机器名 
            //string _myHostName = Dns.GetHostName();
            ////获取本机IP 
            //string _myHostIP = Dns.GetHostEntry(_myHostName).AddressList[1].ToString();
            ////截取IP网段
            //string ipDuan = _myHostIP.Remove(_myHostIP.LastIndexOf('.'));
            ////枚举网段计算机
            //for (int i = 1; i <= 255; i++)
            //{
            //    Ping myPing = new Ping();
            //    myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
            //    string pingIP = ipDuan + "." + i.ToString();
            //    myPing.SendAsync(pingIP, 1000, null);

            //}
            //string s = "";
        }

        //void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
        //{
        //    if (e.Reply.Status == IPStatus.Success)
        //    {
        //        ipList.Add(e.Reply.Address.ToString());                
        //    }
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "PDF文件|*.pdf";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string file = fileDialog.FileName;
                tbFilePath.Text = file;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                tbGenPath.Text = foldPath;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (tbFilePath.Text.Length > 0 && tbGenPath.Text.Length > 0)
            {
                Thread t = new Thread(PDF2JPG);
                t.Start();
            }
            else
            {
                MessageBox.Show("PDF文件与生成目录不得为空", "错误");
            }
        }

        private void PDF2JPG(object obj)
        {
            btnStart.Enabled = false;
            lbStatus.ForeColor = Color.Red;
            lbStatus.Text = "运行中";
            pbStatus.Visible = true;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            PDFKit.PDFSplit2Jpg(tbFilePath.Text, tbGenPath.Text);
            stopwatch.Stop();
            lbTime.Text = stopwatch.Elapsed.ToString();
            btnStart.Enabled = true;
            lbStatus.ForeColor = Color.Green;
            lbStatus.Text = "已转换";
            pbStatus.Visible = false;           
        }   
    }
}
